package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.TArticle;
import com.ruoyi.system.domain.TFrontMenu;
import com.ruoyi.system.service.ITArticleService;
import com.ruoyi.system.service.ITFrontMenuService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 前端内容管理 Controller
 *
 * @author ruoyi
 * @date 2025-05-12
 */
@RestController
@RequestMapping("/system/frontAdmin")
public class FrontAdminController extends BaseController {
    @Resource
    private ITFrontMenuService tFrontMenuService;
    @Resource
    private ITArticleService articleService;

    /**
     * 查询前端菜单列表
     */
    @GetMapping("/getOne/{menuId}")
    public AjaxResult heixieGsjj(@PathVariable Long menuId) {
        TArticle tArticle = new TArticle();
        tArticle.setFrontMenuId(menuId);
        List<TArticle> tArticles = articleService.selectTArticleList(tArticle);
        TArticle article = null;
        if (CollectionUtils.isNotEmpty(tArticles)) {
            article = tArticles.get(0);
        }
        return AjaxResult.success(article);
    }

    /**
     * 查询前端菜单列表
     */
    @PostMapping("/saveArticle")
    public AjaxResult saveArticle(@Validated @RequestBody TArticle tArticle) {
        if (null != tArticle.getId()) {
            articleService.updateTArticle(tArticle);
        } else {
            articleService.insertTArticle(tArticle);
        }
        return AjaxResult.success();
    }

    /**
     * 查询文章管理列表
     */
    @GetMapping("/listArticle")
    public TableDataInfo list(TArticle tArticle) {
        startPage();
        List<TArticle> list = articleService.selectTArticleList(tArticle);
        return getDataTable(list);
    }

    /**
     * 删除文章管理
     */
    @DeleteMapping("/delete/{id}")
    public AjaxResult remove(@PathVariable String id) {
        return toAjax(articleService.deleteTArticleByIds(id));
    }

}
